package com.manage.service.impl;

import com.manage.dao.BuildingDao;
import com.manage.dao.HousingDao;
import com.manage.dao.RoomDao;
import com.manage.dao.impl.BuildingDaoImpl;
import com.manage.dao.impl.HousingDaoImpl;
import com.manage.dao.impl.RoomDaoImpl;
import com.manage.pojo.Housing;
import com.manage.pojo.ResultData;
import com.manage.service.HousingService;
import com.manage.utils.DateUtils;
import com.manage.utils.JDBCUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HousingServiceImpl implements HousingService {
    @Override
    public ResultData selectByPage(String page, String limit) {
        //获取分页条件
        int j = Integer.parseInt(page);
        int k = Integer.parseInt(limit);

        //处理数据库数据
        HousingDao housingDao=new HousingDaoImpl();


        try {
            List<Housing> housingAllPage= housingDao.findAll(j,k);
            List<Housing> housingDaoAll = housingDao.findAll();

//          返回结果
            return new ResultData(0,"查询成功",housingAllPage,housingDaoAll.size());

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return  new ResultData(10000,"数据异常");
        }
    }

    @Override
    public ResultData removeOneByHid(String hid) {
        if (hid==null||hid.isEmpty()){
         return    new ResultData(10000,"数据有误");
        }
        int h = Integer.parseInt(hid);
        HousingDao housingDao=new HousingDaoImpl();
        BuildingDao buildingDao=new BuildingDaoImpl();
        RoomDao roomDao=new RoomDaoImpl();

        int i = 0,i1=0,i2=0;
        try {
            JDBCUtils.begin();
            i = housingDao.deleteHousingByHid(h);
             i1 = buildingDao.deleteBuildingByHid(h);
             i2 = roomDao.deleteroomByHid(h);
            JDBCUtils.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            JDBCUtils.rollBack();
            return new ResultData(10000,"删除异常");
        }
        if(i==0){
            return new ResultData(10000,"删除失败");
        }
        return new ResultData(0,"共删除"+i+"条小区数据"+i1+"条楼栋数据"+i2+"条房间数据");
    }

    @Override
    public ResultData addHousing(Housing housing) {
        ResultData resultData = null;
        try {
            JDBCUtils.begin();
            HousingDao housingDao=new HousingDaoImpl();
            int count = housingDao.insertHousing(housing);

            if(count > 0){
                resultData = new ResultData(0,"添加成功");
            }else{
                resultData = new ResultData(10000,"添加失败");
            }
            JDBCUtils.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            JDBCUtils.rollBack();
            resultData = new ResultData(10000,"添加异常");
        }
        return resultData;
    }

    @Override
    public ResultData deleteAllEmp(List<Housing> housingList) {

        JDBCUtils.begin();
        int a = 0, b = 0, c = 0;
        try {
            for (Housing housing : housingList) {
                int hid = housing.getHid();

            if (hid <= 0) {
                return new ResultData(10000, "数据有误");
            }

            HousingDao housingDao = new HousingDaoImpl();
            BuildingDao buildingDao = new BuildingDaoImpl();
            RoomDao roomDao = new RoomDaoImpl();



                int i = housingDao.deleteHousingByHid(hid);
                int j = buildingDao.deleteBuildingByHid(hid);
                int k = roomDao.deleteroomByHid(hid);
                if (i == 0) {
                    return new ResultData(10000, "删除失败");
                }
                a += i;
                b += j;
                c += k;
            }
                JDBCUtils.commit();
                return new ResultData(0, "共删除" + a + "条小区数据" + b + "条楼栋数据" + c + "条房间数据");

            } catch (SQLException throwables) {
                throwables.printStackTrace();
                JDBCUtils.rollBack();
                return new ResultData(10000, "删除异常");
            }
        }

    @Override
    public ResultData findOne(String hid) {
        if(hid == null || hid.isEmpty()){
            return new ResultData(10000,"参数错误");
        }
        try {
            HousingDao housingDao=new HousingDaoImpl();
            Housing housing = housingDao.selectOne(Integer.parseInt(hid));

            return  new ResultData(0,"查询成功",housing);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return new ResultData(10000,"查询失败");
    }

    @Override
    public ResultData updateEmp(Housing housing) {
        ResultData resultData = null;
        try {
            JDBCUtils.begin();
            HousingDao housingDao=new HousingDaoImpl();
            int count = housingDao.updateEmp(housing);

            if(count > 0){
                resultData = new ResultData(0,"修改成功");
            }else{
                resultData = new ResultData(10000,"修改失败");
            }
            JDBCUtils.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            JDBCUtils.rollBack();
            resultData = new ResultData(10000,"修改异常");
        }
        return resultData;
    }

    @Override
    public ResultData addHousingList(List<String[]> data) {
        if(data == null || data.size() == 0){
            return new ResultData(10000,"上传数据不为能为null");
        }
        JDBCUtils.begin();
        for (String[] d : data) {
            //提醒：此时的这个数组的顺序是要严格要求的
            String hnumber=d[0];
            String hname=d[1];
            String hplace=d[2];
            double placearea= Double.parseDouble(d[3]);
            double greeningrate= Double.parseDouble(d[4]);
            String himage=d[5];
            String developer=d[6];
            String property=d[7];
            Date hhiredate=DateUtils.stringToDate(d[8],"yyyy-MM-dd");

            Housing housing = new Housing(hnumber,hname,hplace,placearea,greeningrate,himage,developer,property,hhiredate);
            try {
                HousingDao housingDao=new HousingDaoImpl();
                int count = housingDao.insertHousing(housing);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                JDBCUtils.rollBack();
                return new ResultData(1000,"导入小区失败！！！");
            }
        }
        JDBCUtils.commit();
        return new ResultData(0,"导入成功"+data.size()+"个小区信息");
    }

    @Override
    public List<String[]> getData() {
        //查询所有小区信息
        HousingDao housingDao=new HousingDaoImpl();
        try {
            List<Housing> housingDaoAll = housingDao.findAll();
            List<String[]> listData = new ArrayList<>();
            for (Housing housing : housingDaoAll) {
                String[] data = new String[12];
                data[0]= String.valueOf(housing.getHid());
                data[1]=housing.getHnumber();
                data[2]=housing.getHname();
                data[3]=housing.getHplace();
                data[4]= String.valueOf(housing.getPlacearea());
                data[5]= String.valueOf(housing.getBcount());
                data[6]= String.valueOf(housing.getRcount());
                data[7]= String.valueOf(housing.getGreeningrate());
                data[8]=housing.getHimage();
                data[9]=housing.getDeveloper();
                data[10]=housing.getProperty();
                data[11]= String.valueOf(housing.getHhiredate());
                listData.add(data);
            }
            return listData;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    @Override
    public ResultData searchAll(String page, String limit, String hname, String hhiredate) {
        try {
            HousingDao housingDao=new HousingDaoImpl();
            List<Housing> housingList = housingDao.selectBySearch(Integer.parseInt(page),Integer.parseInt(limit),hname,hhiredate);
            if(housingList != null && housingList.size() > 0){
                int i = housingDao.selectBySearchCount(hname, hhiredate);
                return new ResultData(0,"查询成功",housingList,i);
            }else{
                return new ResultData(10000,"暂无数据");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return new ResultData(10000,"接口错误");
    }
}
